package com.example.libpro.MyActivity;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.libpro.MyActivity.adminActivity.AdminMainActivity;
import com.example.libpro.R;
import com.example.libpro.Service.LoginService;

public class LoginActivity extends AppCompatActivity {
    private LoginService loginService;
    private boolean loginServiceBound;
    private EditText etAccount;
    private EditText etPassword;
    private Button btnLogin;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            LoginService.LocalBinder binder = (LoginService.LocalBinder) service;
            loginService = binder.getService();
            loginServiceBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            loginServiceBound = false;
        }
    };
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        Log.i("LoginActivity", "onCreate: Activity 初始化完成，开始绑定 LoginService");
        Intent intent = new Intent(this, LoginService.class);// 绑定 Service
        bindService(intent, connection, Context.BIND_AUTO_CREATE); // 第三个参数：BIND_AUTO_CREATE 表示若 Service 未启动则自动创建
        etAccount = findViewById(R.id.etAccount);
        etPassword = findViewById(R.id.etPassword);
        btnLogin = findViewById(R.id.btnLogin);
        setLoginClickListener();
    }
    // 设置登录按钮点击事件
    private void setLoginClickListener() {
        btnLogin.setOnClickListener(v -> {
            // 3.1 读取输入框内容（去除空格）
            String account = etAccount.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            // 3.2 简单校验输入
            if (account.isEmpty() || password.isEmpty()) {
                Toast.makeText(this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            // 3.3 调用LoginService的登录方法（需判断Service是否已绑定）
            if (loginServiceBound && loginService != null) {
                // 调用Service的登录方法（假设LoginService有login(String, String)方法）
                boolean isLoginSuccess = loginService.login(account, password).isSuccess();

                // 根据登录结果提示
                if (isLoginSuccess) {
                    Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                    // 可添加跳转页面逻辑，如：startActivity(new Intent(this, MainActivity.class));
                    startActivity(new Intent(this, AdminMainActivity.class));

                } else {
                    Toast.makeText(this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(this, "服务未连接，请稍后重试", Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    protected void onPause() {
        super.onPause(); // 必须先调用父类方法
        // 清空输入框内容
        etAccount.setText("");
        etPassword.setText("");
    }
}